package org.yn.mkt.controller;

import org.yn.base.util.LoginContext;
import org.yn.emp.domain.Employee;
import org.yn.mkt.dto.ActivityDTO;
import org.yn.mkt.service.IActivityService;
import org.yn.mkt.domain.Activity;
import org.yn.mkt.query.ActivityQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@PermissionAnnotation(name="activity管理",descs = "activity管理")
@Api("activity管理接口")
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    public IActivityService activityService;


    /**
     * 保存和修改公用的
     * @param activity  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="保存和修改Activity",descs = "保存和修改Activity")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Activity activity, HttpServletRequest request){
        Employee loginUser = LoginContext.getLoginUser(request);
        try {
            if( activity.getId()!=null)
                activityService.update(activity,loginUser);
            else
                activityService.insert(activity,loginUser);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除Activity",descs = "删除Activity")
    @ApiOperation(value = "删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            activityService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="获取单个Activity",descs = "获取单个Activity")
    @ApiOperation(value = "获取单个Activity")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Activity activity = activityService.selectById(id);
            return AjaxResult.success(activity);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name="获取所有Activity",descs = "获取所有Activity")
    @ApiOperation(value = "获取所有Activity")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Activity> list = activityService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询Activity",descs = "分页查询Activity")
    @ApiOperation(value = "分页查询Activity")
    @PutMapping
    public AjaxResult queryPage(@RequestBody ActivityQuery query)
    {
        try {
            PageInfo<Activity> pageInfo = activityService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除Activity",descs = "批量删除Activity")
    @ApiOperation(value = "批量删除Activity")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                activityService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }

    /**
     * 活动绑定线索中间表
     * @param activityDTO  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="活动绑定线索中间表",descs = "活动绑定线索中间表")
    @ApiOperation(value = "活动绑定线索中间表")
    @PostMapping("/clue")
    public AjaxResult activityBindClue(@RequestBody ActivityDTO activityDTO){
        try {
            activityService.activityBindClue(activityDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }

}
